package com.example.activitispringbootstest.service.impl;

import com.example.activitispringbootstest.service.TaskListenerService;
import org.activiti.engine.TaskService;
import org.activiti.engine.delegate.DelegateTask;
import org.activiti.engine.impl.persistence.entity.TaskEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;

/**
 * @author swxu_2005@163.com
 * @date 2019/2/3
 */
@Service
public class TaskListenerServiceImpl implements TaskListenerService {

  @Autowired
  private TaskService taskService;

  @Override
  public void findCandidatesByRole(TaskEntity task, String roleName) {
    List<String> candidates = null;
    switch (roleName) {
      case "zg": candidates = Arrays.asList("ZG-001", "ZG-002", "ZG-003");
    }
    task.addCandidateUsers(candidates);
    taskService.saveTask(task);
  }

  @Override
  public void findAssigneeByRole(TaskEntity task, String roleName) {
    String assigneeId = null;
    switch (roleName) {
      case "jl": assigneeId = "JL-001"; break;
      case "boss": assigneeId = "BOSS-001"; break;
    }
    task.setAssignee(assigneeId);
    taskService.saveTask(task);
  }
}
